package com.kun.rental.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.Violation;
import com.kun.rental.mapper.ViolationMapper;
import com.kun.rental.service.IViolationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Service
public class ViolationServiceImpl extends ServiceImpl<ViolationMapper, Violation> implements IViolationService {


    @Autowired
    private ViolationMapper violationMapper;

    /**
     * 分页查询
     * @param page
     * @param violation
     * @return
     */
    @Override
    public Page<Violation> searchByPage(Page<Violation> page, Violation violation) {
        QueryWrapper<Violation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(violation.getNum()),
                "carrental_num", violation.getNum());
        queryWrapper.like(ObjectUtil.isNotEmpty(violation.getReason()),
                "reason",violation.getReason());
        queryWrapper.like(ObjectUtil.isNotEmpty(violation.getLocation()),
                "location",violation.getLocation());
        queryWrapper.le(ObjectUtil.isNotEmpty(violation.getHighViolationTime()),
                "violation_time",violation.getHighViolationTime());
        queryWrapper.ge(ObjectUtil.isNotEmpty(violation.getHighViolationTime()),
                "violation_time",violation.getLowViolationTime());
        queryWrapper.le(ObjectUtil.isNotEmpty(violation.getHighFine()),
                "fine",violation.getHighFine());
        queryWrapper.ge(ObjectUtil.isNotEmpty(violation.getLowFine()),
                "fine",violation.getLowFine());
        queryWrapper.eq(ObjectUtil.isNotEmpty(violation.getStatus()),
                "status",violation.getStatus());
        queryWrapper.orderByDesc("violation_time");
        return violationMapper.selectPage(page,queryWrapper);
    }


    /**
     * 删除及批量删除
     * @param ids
     * @return
     */
    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()){
            return violationMapper.deleteBatchIds(list)>0;
        }
        return false;
    }
}
